package com.ll.menu.user.history.detail;

import android.app.Activity;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.droid.library.app.App;
import com.droid.library.widget.recyclerview.BaseRecyclerAdapter;
import com.droid.library.widget.recyclerview.BaseViewHolder;
import com.ll.menu.user.R;
import com.ll.menu.user.app.entity.Food;

/**
 * 用餐记录Adapter
 *
 * @author wangheng
 */

public class HistoryDetailAdapter extends BaseRecyclerAdapter<Food> {


    public HistoryDetailAdapter(Activity activity){
        super(activity);
    }

    @Override
    protected BaseViewHolder onCreateRealViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = mActivity.getLayoutInflater();
        View view = inflater.inflate(R.layout.item_history_detail,parent,false);
        return new Holder(mActivity,view, HistoryDetailAdapter.this);
    }

    static class Holder extends BaseViewHolder<Food>{
        TextView nameTextView;
        TextView currentPriceTextView;
        TextView oldPriceTextView;
        TextView numberTextView;
        TextView statusTextView;


        private String mCurrentPriceMode = null;
        private String mOldPriceMode = null;
        private String mCountMode = null;

        private int colorGray;
        private int colorOrange;
        private int colorGreen;


        public Holder(Activity activity, View itemView, HistoryDetailAdapter adapter) {
            super(itemView,activity,adapter);

            colorGray = App.getInstance().getResources().getColor(R.color.color_gray);
            colorOrange = App.getInstance().getResources().getColor(R.color.color_orange);
            colorOrange = App.getInstance().getResources().getColor(R.color.color_green);

            mCurrentPriceMode = App.getInstance().getString(R.string.cart_current_price_mode);
            mOldPriceMode = App.getInstance().getString(R.string.cart_old_price_mode);
            mCountMode = App.getInstance().getString(R.string.order_number_mode);

            nameTextView = (TextView) itemView.findViewById(R.id.tvHistoryDetailFoodName);
            currentPriceTextView = (TextView) itemView.findViewById(R.id.tvHistoryDetailCurrentPrice);
            oldPriceTextView = (TextView) itemView.findViewById(R.id.tvHistoryDetailOldPrice);
            numberTextView = (TextView) itemView.findViewById(R.id.tvHistoryDetailNumber);
            statusTextView = (TextView) itemView.findViewById(R.id.tvHistoryDetailStatus);

        }

        @Override
        public void onBindViewHolder(int position, Food item) {

            // 名称
            nameTextView.setText(item.getName());

            // 现价
            currentPriceTextView.setText(String.format(mCurrentPriceMode,item.getCurrentPrice()));

            // 原价
            if(item.getOldPrice() != item.getCurrentPrice()) {
                oldPriceTextView.setVisibility(View.VISIBLE);
                oldPriceTextView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                oldPriceTextView.setText(String.format(mOldPriceMode, item.getOldPrice()));
            }else{
                oldPriceTextView.setVisibility(View.GONE);
            }

            // 数量
            numberTextView.setText(String.format(mCountMode,item.getCount()));

            // 状态
            if(item.isDiscount()){
                statusTextView.setBackgroundColor(colorGreen);
                statusTextView.setText(R.string.order_history_detail_discount);
            } else{
                if(item.isReturn()){
                    statusTextView.setBackgroundColor(colorGray);
                    statusTextView.setText(R.string.order_history_detail_return);
                }else if(item.isSale()){
                    statusTextView.setBackgroundColor(colorGray);
                    statusTextView.setText(R.string.order_history_detail_sale);
                }else{
                    statusTextView.setBackgroundColor(colorOrange);
                    statusTextView.setText(R.string.order_history_detail_sale_pick);
                }
            }

        }
    }
}
